package cn.edu.scau.cmi.linyiqu.javaFX;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.linyiqu.dao.StudentDAO;
import cn.edu.scau.cmi.linyiqu.dao.TeacherDAO;
import cn.edu.scau.cmi.linyiqu.domain.Student;
import cn.edu.scau.cmi.linyiqu.domain.Teacher;
import cn.edu.scau.cmi.linyiqu.util.ApplicationContextUtil;
import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.control.Label;

import javafx.scene.control.TextArea;

import javafx.scene.control.ChoiceBox;

public class HibernateController {
	@FXML
	private TextArea findText;
	@FXML
	private Label findsLabel;
	@FXML
	private TextField findsInput;
	@FXML
	private TextField findtInput;
	@FXML
	private Label findtLabel;
	@FXML
	private Button findsButton;
	@FXML
	private Button findtButton;
	@FXML
	private ChoiceBox<String> findsChoice;
	@FXML
	private ChoiceBox<String> findtChoice;
	@FXML
	private Label addsLabel;
	@FXML
	private Label addtLabel;
	@FXML
	private TextField addsnameInput;
	@FXML
	private TextField addtnameInput;
	@FXML
	private Button addsButton;
	@FXML
	private Button addtButton;
	@FXML
	private TextField addsidInput;
	@FXML
	private TextField addtidInput;
	@FXML
	private TextArea addText;
	@FXML
	private Label delsLabel;
	@FXML
	private Label deltLabel;
	@FXML
	private TextField delsInput;
	@FXML
	private TextField deltInput;
	@FXML
	private Button delsButton;
	@FXML
	private Button deltButton;
	@FXML
	private TextArea delText;
	@FXML
	private ChoiceBox<String> mansChoice;
	@FXML
	private ChoiceBox<String> mantChoice;
	@FXML
	private ChoiceBox<String> mansSName;
	@FXML
	private ChoiceBox<String> mansTName;
	@FXML
	private ChoiceBox<String> mantTName;
	@FXML
	private ChoiceBox<String> mantSName;
	@FXML
	private Button mansButton;
	@FXML
	private Button mantButton;
	@FXML
	private TextArea manText;
	@FXML
	private TextArea listStudents;
	@FXML
	private TextArea listTeachers;

	private StudentDAO studentDAO;
	private TeacherDAO teacherDAO;

	private List<Student> students;
	private List<Teacher> teachers;

	private List<String> snameList = new ArrayList();
	private List<String> tnameList = new ArrayList();

	private List<String> getStudentItems() {
		students = studentDAO.findAll();

		for (Student student : students) {
			snameList.add(student.getName());
		}
		return snameList;
	}

	private List<String> getTeacherItems() {
		teachers = teacherDAO.findAll();

		for (Teacher teacher : teachers) {
			tnameList.add(teacher.getName());
		}
		return tnameList;
	}

	public void initialize() {
		ApplicationContext ac = getApplicationContext();
		studentDAO = (StudentDAO) ac.getBean("studentDAO");
		teacherDAO = (TeacherDAO) ac.getBean("teacherDAO");
		findButtonClick();
		addButtonClick();
		delButtonClick();
		manButtonClick();
		findsChoice.getItems().addAll("学号查询", "姓名查询");
		findtChoice.getItems().addAll("工号查询", "姓名查询");
		mansChoice.getItems().addAll("更改导师", "删除导师", "增加导师");
		mantChoice.getItems().addAll("删除学生", "增加学生");

		mansSName.getItems().addAll(getStudentItems());
		mansTName.getItems().addAll(getTeacherItems());
		
		mantSName.getItems().addAll(mansSName.getItems());
		mantTName.getItems().addAll(mansTName.getItems());

		refreshList();
	}

	public void refreshList() {

		listStudents.clear();
		listTeachers.clear();
		listStudents.appendText("学生列表：\n\n");
		listTeachers.appendText("老师列表：\n\n");

		students = studentDAO.findAll();
		teachers = teacherDAO.findAll();

		for (Student student : students) {
			listStudents.appendText(student.toString());
		}

		for (Teacher teacher : teachers) {
			listTeachers.appendText(teacher.toString());
		}
	}

	public void manButtonClick() {
		mansButton.setOnAction(e -> {
			String choice = mansChoice.getValue();
			String sName = mansSName.getValue();
			if (choice == "更改导师") {
				if (mansSName.getValue() != null && mansTName.getValue() != null) {
					try {
						List<Student> students = studentDAO.findByName(sName);
						List<Teacher> teachers = teacherDAO.findByName(mansTName.getValue());
						Student student=students.get(0);
						student.setTeacher(teachers.get(0));
						studentDAO.update(student);
						manText.appendText("成功为学生： "+ student.getName() + "  更改导师: " + student.getTeacher().getName()+ "\n\n");
						refreshList();
					} catch (Exception e2) {
						manText.appendText("修改失败\n\n");
					}
				}else {
					manText.appendText("请检查输入\n\n");

				}

			} else if (choice == "删除导师") {
				if(mansSName.getValue() != null) {
					try {
						List<Student> students = studentDAO.findByName(sName);
						Student student=students.get(0);
						student.setTeacher(null);
						studentDAO.update(student);	
						manText.appendText("成功删除学生： "+ student.getName() +  "的导师\n\n");
						refreshList();
					}catch (Exception e2) {
						manText.appendText("删除失败\n\n");
					}
				}else {
					manText.appendText("请检查输入\n\n");
				}

			} else if (choice == "增加导师") {
				if(mansSName.getValue() != null && mansTName.getValue() != null) {
					try {
						List<Student> students = studentDAO.findByName(sName);
						Student student=students.get(0);
						if(student.getTeacher()==null) {
							List<Teacher> teachers = teacherDAO.findByName(mansTName.getValue());
							student.setTeacher(teachers.get(0));
							studentDAO.update(student);
							manText.appendText("成功为学生： "+ student.getName() + "  添加导师: " + student.getTeacher().getName()+ "\n\n");
							refreshList();
						}else {
							manText.appendText("该生已有导师\n\n");
						}
					}catch (Exception e2) {
						manText.appendText("增加失败\n\n");
					}
				}else {
					manText.appendText("请检查输入\n\n");
				}

			} else {
				manText.appendText("请选择\n\n");
			}
		});
		
		
		
		
		mantButton.setOnAction(e -> {
			String choice = mantChoice.getValue();
			String tName = mantTName.getValue();
			String sName = mantSName.getValue();
			if (choice == "删除学生") {
				if((tName!= null) && (sName!=null)) {
					try {
						List<Student> students = studentDAO.findByName(sName);
						List<Teacher> teachers = teacherDAO.findByName(tName);
						Teacher teacher = teachers.get(0);
						Student student=students.get(0);
						if((student.getTeacher().getName()).equals(tName)) { //学生的老师是选择的老师
							student.setTeacher(null);
							studentDAO.update(student);	
							teacherDAO.update(teacher);
							refreshList();
							manText.appendText("成功为老师： " + tName + "  删除学生： " + sName + "\n\n");
						}else {
							manText.appendText(sName + "  不是" + tName + "的学生\n\n" );
						}
						
					}catch (Exception e2) {
						manText.appendText("删除失败\n\n");
					}
				}else {
					manText.appendText("请检查输入\n\n");
				}

			} else if (choice == "增加学生") {
				if((tName!= null) && (sName!=null)) {
					try {
						List<Student> students = studentDAO.findByName(sName);
						Student student=students.get(0);
						if(student.getTeacher()==null) {
							List<Teacher> teachers = teacherDAO.findByName(tName);
							student.setTeacher(teachers.get(0));
							studentDAO.update(student);
							manText.appendText("成功为导师： "+ tName + "  添加学生:  " + sName + "\n\n");
							refreshList();
						}else {
							manText.appendText("该生已有导师\n\n");
						}
					}catch (Exception e2) {
						manText.appendText("增加失败\n\n");
					}
				}else {
					manText.appendText("请检查输入\n\n");
				}

			} else {
				manText.appendText("请选择\n\n");
			}
		});
	}

	public void findButtonClick() {
		findsButton.setOnAction(e -> {
			String choice = findsChoice.getValue();
			if (choice == "学号查询") {
				String idString = findsInput.getText();

				if (!idString.equals("")) {
					Long id = Long.parseLong(idString);
					Student student = studentDAO.findById(id);
					if (student != null) {
						// String name = student.getName();
						// findText.appendText("学号为："+id +"\t\t姓名: " + name + "\t\t导师： "+
						// student.getTeacher()+"\n\n");
						findText.appendText(student.toString());
						findText.appendText("\n\n");

					} else {
						findText.appendText("查询失败！学号不存在\n\n");
					}
				} else {
					findText.appendText("请检查输入\n\n");
				}
			} else if (choice == "姓名查询") {
				String name = findsInput.getText();
				if (!name.equals("")) {
					List<Student> students = studentDAO.findByName(name);
					if (students != null) {
						for (Student student : students) {
							findText.appendText(student.toString());
						}
					} else {
						findText.appendText("查询失败！该学生不存在\n\n");
					}
				} else {
					findText.appendText("请检查输入\n\n");
				}
			} else {
				findText.appendText("请选择\n\n");
			}

		});

		findtButton.setOnAction(e -> {
			String choice = findtChoice.getValue();
			if (choice == "工号查询") {
				String idString = findtInput.getText();
				Long id = Long.parseLong(idString);
				if (!idString.equals("")) {
					Teacher teacher = teacherDAO.findById(id);
					if (teacher != null) {
//						String name = teacher.getName();
//						findText.appendText("工号为："+id +"\t\t姓名: " + name +"\n\n");
						findText.appendText(teacher.toString());
						findText.appendText("\n\n");
					} else {
						findText.appendText("查询失败！工号不存在\n\n");
					}
				} else {
					findText.appendText("请检查输入\n\n");
				}
			} else if (choice == "姓名查询") {
				String name = findtInput.getText();
				if (!name.equals("")) {
					List<Teacher> teachers = teacherDAO.findByName(name);
					if (teachers != null) {
						for (Teacher teacher : teachers) {
							findText.appendText(teacher.toString());
						}
					} else {
						findText.appendText("查询失败！该老师不存在\n\n");
					}
				} else {
					findText.appendText("请检查输入\n\n");
				}

			} else {
				findText.appendText("请选择\n\n");
			}

		});

	}

	public void addButtonClick() {
		addsButton.setOnAction(e -> {
			String idString = addsidInput.getText();
			String name = addsnameInput.getText();
			Student student = new Student();
			Long id = Long.parseLong(idString);
			if (studentDAO.findById(id) == null) {

				student.setId(id);
				student.setName(name);
				studentDAO.save(student);
				addText.setText("成功添加学生\n\n学号： " + id + "\t\t姓名： " + name + "\n\n");

			} else {
				addText.setText("学号为 " + idString + "的同学已存在!\n\n");
			}
			refreshList();

		});

		addtButton.setOnAction(e -> {
			String idString = addtidInput.getText();
			String name = addtnameInput.getText();
			Teacher teacher = new Teacher();
			Long id = Long.parseLong(idString);
			if (teacherDAO.findById(id) == null) {

				teacher.setId(id);
				teacher.setName(name);
				teacherDAO.save(teacher);
				addText.appendText("成功添加老师\n\n工号： " + id + "\t\t姓名： " + name + "\n");

			} else {
				addText.setText("工号为 " + idString + "的老师已存在!\n\n");
			}
			refreshList();
		});

	}

	public void delButtonClick() {
		delsButton.setOnAction(e -> {
			String idString = delsInput.getText();

			if (idString != null) {
				try {
					Long id = Long.parseLong(idString);
					Student student = studentDAO.findById(id);
					if (student != null) {
						String name = student.getName();
						studentDAO.delete(student);
						delText.appendText("成功删除学生: " + id + name + "\n\n");
//						delsInput.setText("");
					} else {
						delText.appendText("删除失败！该学号不存在\n\n");
					}
				} catch (Exception e2) {
					delText.appendText("请检查输入是否正确！\n\n");
				}
			}

			refreshList();
		});

		deltButton.setOnAction(e -> {
			String idString = deltInput.getText();

			if (!idString.equals("")) {
				try {
					Long id = Long.parseLong(idString);
					Teacher teacher = teacherDAO.findById(id);
					if (teacher != null) {
						String name = teacher.getName();
						teacherDAO.delete(teacher);
						delText.appendText("成功删除教师: " + id + name + "\n\n");
//						deltInput.setText("");
					} else {
						delText.appendText("删除失败！该工号不存在\n\n");
					}
				} catch (Exception e2) {
					delText.appendText("请检查输入是否正确！\n\n");
				}
			}

			refreshList();
		});
	}

	private static ApplicationContext getApplicationContext() {
		ApplicationContext applicationContext=ApplicationContextUtil.getApplicationContext();
		return applicationContext;
	}
}
